//
//  ReaderViewController.m
//  Reader
//
//  Created by cjz on 2016/12/15.
//  Copyright © 2016年 wclwangchenglong. All rights reserved.
//

#import "ReaderViewController.h"
#import "ViewController.h"
#import "ContentViewController.h"


@interface ReaderViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
{
    UIPageViewController *page;
    CGFloat totalHeight;
    NSUInteger totalPages;
    NSUInteger currentPage;
    NSUInteger lengthPerPage;
    UIBarButtonItem *barItem;



}

@end

@implementation ReaderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    page = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey : @(UIPageViewControllerSpineLocationNone)}];
    page.dataSource = self;
    page.delegate = self;
    [self addChildViewController:page];
    page.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 44);
    
        [self.view addSubview:page.view];
    
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[[UILabel new].font fontWithSize:24.0]} context:nil];
    totalHeight = ceil(rect.size.height);
    totalPages = ceil(totalHeight / (self.view.frame.size.height - 64 - 44));
    lengthPerPage = _text.length / totalPages;
    currentPage = 0;
    
    barItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"第%u页／共%u页",currentPage + 1, totalPages] style:UIBarButtonItemStylePlain target:nil action:nil];
    //    toolBar.items = @[barItem];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"尾页" style:UIBarButtonItemStylePlain target:self action:@selector(gotoLast:)];
    self.toolbarItems = @[barItem, item];
    
    
    ContentViewController *con = [ContentViewController new];
    con.text = [_text substringWithRange:NSMakeRange(currentPage * lengthPerPage, lengthPerPage)];
    [page setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    

}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.toolbarHidden = NO;
}
- (void)gotoLast:(id)sender
{
    currentPage = totalPages - 1;
    ContentViewController *con = [self getCurContentController];
    [page setViewControllers:@[con] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    [self refreshBarItemShow];
}

- (void)refreshBarItemShow
{
    barItem.title = [NSString stringWithFormat:@"第%u页／共%u页",currentPage + 1, totalPages];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (currentPage <= 0) {
        return nil;
    }
    currentPage--;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if (currentPage >= totalPages - 1) {
        return nil;
    }
    currentPage++;
    [self refreshBarItemShow];
    return [self getCurContentController];
}

- (ContentViewController *)getCurContentController
{
    ContentViewController *con = [ContentViewController new];
    con.text = [_text substringWithRange:NSMakeRange(currentPage * lengthPerPage, lengthPerPage)];
    con.index = currentPage;
    return con;
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    
    if (!completed) {
        ContentViewController *con = (ContentViewController *)previousViewControllers[0];
        currentPage = con.index;
        [self refreshBarItemShow];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
